﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCT.UI.ChildWindows
{
    public partial class Sms : EFViewCmes
    {
        public Sms(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }

        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 是否给临时人员发送短信
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdIsTemporary_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null && e.OldValue.Equals(""))
            {
                return;
            }
            if (this.fdIsTemporary.Value.Equals("否"))
            {
                this.fdName.Visibility = Visibility.Collapsed;
                this.fdMobilePhone.IsReadOnly = true;
                this.choiceName.Visibility = Visibility.Visible;
                (this.choiceName.Content as CmesCustomView).Display= this.fdName.Value;
                this.fdMobilePhone.Label = " 手机号码";
                this.fdMobilePhone.eRegex = "";
                this.fdMobilePhone.eValidateErrorPrompt = "";
                this.fdName.eBindingName = "result-0-choiceName";
                this.choiceName.eBindingName = "result-0-fdName";
            }
            else
            {
                this.choiceName.Visibility = Visibility.Collapsed;
                this.fdMobilePhone.IsReadOnly = false;
                this.fdName.Visibility = Visibility.Visible;
                this.fdMobilePhone.Label = "*手机号码";
                this.fdMobilePhone.eRegex = "^1[0-9]{10}$";
                this.fdMobilePhone.eValidateErrorPrompt = "输入11个字符，不能输入空格。必须以1开头";
                this.fdName.eBindingName = "result-0-fdName";
                this.choiceName.eBindingName = "result-0-choiceName";
            }
            EFDataField field = this.choiceName as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;

            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
//                    this.fdEquipmentCode.Value = picker.ItemObject.GetType().GetProperty("fdCode").GetValue(picker.ItemObject, null) as string;
//                    this.fdDepartment.Value = picker.ItemObject.GetType().GetProperty("fdDepartment").GetValue(picker.ItemObject, null) as string;
                }
                //没选中时赋予空字符串
                else
                {
//                    this.fdEquipmentCode.Value = "";
//                    this.fdDepartment.Value = "";
                }
            }

        }
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            this.fdCreateTime.Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            this.fdStatus.Value = "未发送";
            if(this.fdName.Value==null || this.fdName.Value.Equals("")){
                this.fdName.Value=this.choiceName.Value;
            }
            if (this.choiceName.Value == null) 
            {
                this.choiceName.Value = "";
            }
            base.OnCommitButtonClick("ZMCT30", sender, e);
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 发送按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SendClick(object sender, RoutedEventArgs e)
        {
            this.fdCreateTime.Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            this.fdSendTime.Value = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
            this.fdStatus.Value = "已发送";
            base.OnCommitButtonClick("ZMCT30", sender, e);
        }

        //choiceName_ValueChanged
        private void choiceName_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            EFDataField field = this.choiceName as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;

            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdNo.Value = (picker.ItemObject as EIInfo).GetBlock("VALUES").GetRow(0)["fdNo"];
                    this.fdMobilePhone.Value = (picker.ItemObject as EIInfo).GetBlock("VALUES").GetRow(0)["fdPhone"]; 
                }
                //没选中时赋予空字符串
                else
                {
                    this.fdNo.Value = "";
                    this.fdMobilePhone.Value = "";
                }
            }
        }

    }
}
